﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSP.MathLibrary;
using System.Data.SqlClient;
using System.Data;

namespace DataLayer
{
    public class NWPDataManager
    {
#if remote
        private static string _connectionString = @"server = 127.0.0.1,1433; database = trueWrf;uid = dt; pwd=dt;Trusted_Connection=no";
#else
        private static string _connectionString = @"server = 192.168.116.101; database = trueWrf;uid = dt; pwd=dt;Trusted_Connection=no";
#endif



        public static MeteoDataRaw GetList(int stationId, string paramName, DateTime dtBegin, DateTime dtEnd)
        {
            //List<ComplexNumber> result = new List<ComplexNumber>();
            MeteoDataRaw mdr = new MeteoDataRaw();
            mdr.TimeStep = 1.0;

            using (SqlConnection con = new SqlConnection(_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("", con))
                {
                    cmd.CommandText = string.Format(@"SELECT [value]-273.16, forecastdate
                                                      FROM [trueWrf].[dbo].[Values]
                                                        where pointid=@pid and meteoelementid=1   
                                                        and (startdate between @dtBegin and @dtEnd)
                                                        and datepart(hh, startdate) = 12 
                                                        --and datepart(hh, forecastdate) in (0,3,6,9,12,15,18,21) 
                                                        --and datepart(mm, forecastdate) in (10,11,12,1,2,3) 
                                                        and (datediff(hh, startdate, forecastdate) between 24.1 and 48)  and domainid=8 
                                                        order by forecastdate", paramName);
                    cmd.Parameters.AddWithValue("@pid", stationId);
                    cmd.Parameters.AddWithValue("@dtBegin", dtBegin);
                    cmd.Parameters.AddWithValue("@dtEnd", dtEnd);

                    cmd.CommandTimeout = 120;
                    con.Open();

                    IDataReader idr = cmd.ExecuteReader();
                    while (idr.Read())
                    {
                        if (idr[0] is DBNull)
                        {
                            mdr.SourceDataRow.Add(new ObservationPoint { DateTime = Convert.ToDateTime(idr[1]), Value = double.NaN });
                            //result.Add(result[result.Count - 1]);
                        }
                        else
                        {
                            mdr.SourceDataRow.Add(new ObservationPoint { DateTime = Convert.ToDateTime(idr[1]), Value = Convert.ToDouble(idr[0]) });
                            //result.Add(new ComplexNumber(Convert.ToDouble(idr[0]), 0));
                        }
                    }
                    con.Close();
                }
            }

            return mdr;
        }
    }
}
